package client.gui;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.HashMap;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import util.StringUtils;
import util.pref.PrefHandler;

/**
 * The options dialog.
 * @author ernesta
 */
public class Options extends javax.swing.JFrame {
    private static final String KEYS[] = {"USERNAME", "PASSWORD", "CONNECTIONTYPE", "HOSTNAME", "PORT", "REMOTEFOLDER", "LOCALFOLDER", "NAME"};
    private static final String FTP[] = {"FTP", "FTPS", "SFTP"};
    
    private String localLocation;
    
    
    /** Creates new form Options */
    public Options() {
        initComponents();
        populatePreferences();
        indicateEmptyPreferences();
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Options.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Options.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Options.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Options.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Options options = new Options();
                options.setVisible(true);
            }
        });
    }
    
    
    /**
     * Retrieve saved preferences and update the dialog accordingly.
     */
    private void populatePreferences() {
        PrefHandler prefHandler = new PrefHandler();
        HashMap<String, String> preferences = prefHandler.getPreferences(KEYS);
        
        if (preferences != null) {
            usernameFTP.setText(preferences.get(KEYS[0]));
            passwordFTP.setText(preferences.get(KEYS[1]));
        
            if (preferences.get(KEYS[2]).equals(FTP[2])) {
                comboFTP.setSelectedIndex(2);
            } else if (preferences.get(KEYS[2]).equals(FTP[1])) {
                comboFTP.setSelectedIndex(1);
            } else {
                comboFTP.setSelectedIndex(0);
            }

            hostName.setText(preferences.get(KEYS[3]));
            portNumber.setText(preferences.get(KEYS[4]));
            remoteLoc.setText(preferences.get(KEYS[5]));
            usernameVersi.setText(preferences.get(KEYS[7]));
            
            localLocation = preferences.get(KEYS[6]);
        }
    }
    
    
    private void indicateEmptyPreferences() {
        if (collectTextField(usernameVersi).equals(""))
            versiLabel.setForeground(Color.RED);
        if (collectTextField(hostName).equals(""))
            hostLabel.setForeground(Color.RED);
        if (collectTextField(portNumber).equals(""))
            portLabel.setForeground(Color.RED);
        if (collectTextField(remoteLoc).equals(""))
            remoteLabel.setForeground(Color.RED);
        if (localLocation.equals(""))
            localLabel.setForeground(Color.RED);
        if (collectTextField(usernameFTP).equals(""))
            usernameLabel.setForeground(Color.RED);
        if (collectPasswordField(passwordFTP).equals(""))
            passwordLabel.setForeground(Color.RED);
    }
    
    
    /**
     * Get information from specified text fields.
     * @param field
     * @return entered text, or an empty string, if none
     */
    private String collectTextField(JTextField field) {
        if (field.getText() == null) {
            return "";
        } else {
            return field.getText();
        }
    }
    
    
    /**
     * Get information from specified password fields.
     * @param field
     * @return entered password, or an empty string, if none
     */
    private String collectPasswordField(JPasswordField field) {
        if (new String(field.getPassword()) == null) {
            return "";
        } else {
            return new String(field.getPassword());
        }
    }
    
    
    /**
     * Get information from a specified combo box.
     * @param field
     * @return the selected option
     */
    private String collectComboField(JComboBox field) {
        return field.getSelectedItem().toString();
    }
    
    
    /**
     * Save data from each dialog field to a preferences file.
     */
    private void savePreferences() {
        PrefHandler prefHandler = new PrefHandler();
        HashMap<String, String> preferences = new HashMap();
        
        preferences.put(KEYS[0], collectTextField(usernameFTP));
        preferences.put(KEYS[1], collectPasswordField(passwordFTP));
        preferences.put(KEYS[2], collectComboField(comboFTP));
        preferences.put(KEYS[3], collectTextField(hostName));
        preferences.put(KEYS[4], collectTextField(portNumber));
        preferences.put(KEYS[5], collectTextField(remoteLoc));
        preferences.put(KEYS[7], collectTextField(usernameVersi));
        
        prefHandler.setPreferences(preferences);
    }
    
    
    /**
     * Immediately deletes all typed characters that are not digits.
     * @param evt
     * @return the typed character or null, if not digit
     */
    private String allowDigitsOnly(KeyEvent evt) {
        char c = evt.getKeyChar();
        if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
            evt.consume();
            return null;
        } else if (c == KeyEvent.VK_BACK_SPACE) {
            return null;
        } else {
            return String.valueOf(c);
        }
    }
    
    
    /**
     * Immediately deletes all typed characters that are not alphanumeric.
     * @param evt
     * @return the typed character or null, if not alphanumeric
     */
    private String allowAlphanumOnly(KeyEvent evt) {
        char c = evt.getKeyChar();
        if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
            return String.valueOf(c);
        } else if ((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_CONTROL) || (c == KeyEvent.VK_ALT) || (c == KeyEvent.VK_SHIFT) || (c == KeyEvent.META_DOWN_MASK)) {
            return null;
        } else {
            evt.consume();
            return null;
        }
    }
    
    
    /**
     * Immediately deletes the last typed character.
     * @param evt 
     */
    private void deleteLastCharacter(KeyEvent evt) {
        evt.consume();
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        usernameVersi = new javax.swing.JTextField();
        versiLabel = new javax.swing.JLabel();
        hostName = new javax.swing.JTextField();
        hostLabel = new javax.swing.JLabel();
        portLabel = new javax.swing.JLabel();
        remoteLoc = new javax.swing.JTextField();
        portNumber = new javax.swing.JTextField();
        remoteLabel = new javax.swing.JLabel();
        localLabel = new javax.swing.JLabel();
        localLoc = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        usernameFTP = new javax.swing.JTextField();
        passwordFTP = new javax.swing.JPasswordField();
        comboFTP = new javax.swing.JComboBox();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        okButt = new javax.swing.JButton();
        cancelButt = new javax.swing.JButton();

        fileChooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Versi Preferences");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Versi Details"));

        usernameVersi.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                usernameVersiKeyTyped(evt);
            }
        });

        versiLabel.setText("Username");

        hostName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                hostNameKeyTyped(evt);
            }
        });

        hostLabel.setText("Host name");

        portLabel.setText("Port number");

        remoteLoc.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                remoteLocKeyTyped(evt);
            }
        });

        portNumber.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                portNumberKeyTyped(evt);
            }
        });

        remoteLabel.setText("Remote file location");

        localLabel.setText("Local file location");

        localLoc.setText("Change");
        localLoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                localLocActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(versiLabel)
                    .add(hostLabel)
                    .add(remoteLabel)
                    .add(localLabel)
                    .add(portLabel))
                .add(11, 11, 11)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, localLoc, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, remoteLoc, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, portNumber, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, hostName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, usernameVersi, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(new java.awt.Component[] {hostName, localLoc, portNumber, remoteLoc, usernameVersi}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(versiLabel)
                    .add(usernameVersi, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(hostName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(hostLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(portLabel)
                    .add(portNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(remoteLoc, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(remoteLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(localLabel)
                    .add(localLoc)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("FTP Details"));

        usernameFTP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                usernameFTPKeyTyped(evt);
            }
        });

        passwordFTP.setText("jPasswordField1");
        passwordFTP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                passwordFTPKeyTyped(evt);
            }
        });

        comboFTP.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FTP", "FTPS", "SFTP" }));

        usernameLabel.setText("Username");

        passwordLabel.setText("Password");

        jLabel4.setText("Connection mode");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(passwordLabel)
                    .add(jLabel4)
                    .add(usernameLabel))
                .add(26, 26, 26)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, usernameFTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, passwordFTP, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, comboFTP, 0, 144, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(new java.awt.Component[] {comboFTP, passwordFTP, usernameFTP}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(usernameLabel)
                    .add(usernameFTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(passwordFTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(passwordLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(comboFTP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)))
        );

        jPanel2Layout.linkSize(new java.awt.Component[] {comboFTP, passwordFTP, usernameFTP}, org.jdesktop.layout.GroupLayout.VERTICAL);

        okButt.setText("OK");
        okButt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtActionPerformed(evt);
            }
        });

        cancelButt.setText("Cancel");
        cancelButt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(cancelButt)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(okButt)))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {cancelButt, okButt}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, cancelButt)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, okButt))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        layout.linkSize(new java.awt.Component[] {cancelButt, okButt}, org.jdesktop.layout.GroupLayout.VERTICAL);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    /**
     * Save all the changes and dispose of the preferences dialog.
     * @param evt 
     */
    private void okButtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtActionPerformed
        this.savePreferences();
        this.dispose();
        
    }//GEN-LAST:event_okButtActionPerformed

    
    /**
     * Dispose of the preferences dialog, do not save any changes.
     * @param evt 
     */
    private void cancelButtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtActionPerformed

    
    /**
     * On every key typed, check the typed character in the FTP port field.
     * If there are already 5 characters, delete the typed one.
     * If the port number is larger than 65535, color the port label red.
     * Allow numeric characters only, delete all others.
     * @param evt 
     */
    private void portNumberKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_portNumberKeyTyped
        portLabel.setForeground(Color.BLACK);
        
        int port = 0;
        String digit = allowDigitsOnly(evt);
        String entry = portNumber.getText();
        
        if (digit != null && entry.length() < 5) {
            entry += digit;
        } else {
            deleteLastCharacter(evt);
        }
        
        if (!entry.equals("") ) {
            port = Integer.parseInt(entry);
            if (port > 65535) {
                portLabel.setForeground(Color.RED);
            } else {
                portLabel.setForeground(Color.BLACK);
            }
        }
    }//GEN-LAST:event_portNumberKeyTyped

    
    /**
     * On every key typed, check the typed character in the Versi username field.
     * If there are already 10 characters, delete the typed one.
     * Allow alphanumeric characters only, delete all others.
     * @param evt 
     */
    private void usernameVersiKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameVersiKeyTyped
        versiLabel.setForeground(Color.BLACK);
        
        String character = allowAlphanumOnly(evt);
        String username = usernameVersi.getText();
        
        if (character != null) {
            username += character;
        }
        
        if (username.length() > 10) {
            deleteLastCharacter(evt);
        } else if (username.length() == 0) {
            versiLabel.setForeground(Color.RED);
        }
    }//GEN-LAST:event_usernameVersiKeyTyped

    
    /**
     * On every key typed, check the typed character in the FTP username field.
     * If there are already 32 characters, delete the typed one.
     * If the username does not contain pure ASCII, color the username label red.
     * @param evt 
     */
    private void usernameFTPKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameFTPKeyTyped
        usernameLabel.setForeground(Color.BLACK);
        
        String username = usernameFTP.getText();
        if (username.length() > 31) {
            deleteLastCharacter(evt);
        } else {
            username += String.valueOf(evt.getKeyChar());
        }
        
        if (!StringUtils.isPureAscii(username)) {
            usernameLabel.setForeground(Color.RED);
        }
    }//GEN-LAST:event_usernameFTPKeyTyped

    
    /**
     * On every key typed, check the typed character in the FTP password field.
     * If there are already 32 characters, delete the typed one.
     * If the password does not contain pure ASCII, color the password label red.
     * @param evt 
     */
    private void passwordFTPKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFTPKeyTyped
        passwordLabel.setForeground(Color.BLACK);
        
        String password = new String(passwordFTP.getPassword());
        if (password.length() > 31) {
            deleteLastCharacter(evt);
        } else {
            password += String.valueOf(evt.getKeyChar());
        }
        
        if (!StringUtils.isPureAscii(password)) {
            passwordLabel.setForeground(Color.RED);
        }
        
        System.out.println(password);
    }//GEN-LAST:event_passwordFTPKeyTyped

    
    /**
     * Allow the user to choose the preferred local directory for storing code.
     * @param evt 
     */
    private void localLocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_localLocActionPerformed
        PrefHandler prefHandler = new PrefHandler();
        String path = "";
        
        HashMap<String, String> oldPreferences = prefHandler.getPreferences(KEYS);
        if (oldPreferences != null) {
            path = oldPreferences.get(KEYS[6]);
        }
        
        if (!path.equals("")) {
            fileChooser.setCurrentDirectory(new File(path));
        }
        
        if (fileChooser.showDialog(this, "Select") == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            path = file.getAbsolutePath();
            
            HashMap<String, String> newPreferences = new HashMap();
            newPreferences.put(KEYS[6], path);
            prefHandler.setPreferences(newPreferences);
            
            localLabel.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_localLocActionPerformed

    private void hostNameKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_hostNameKeyTyped
        hostLabel.setForeground(Color.BLACK);
    }//GEN-LAST:event_hostNameKeyTyped

    private void remoteLocKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_remoteLocKeyTyped
        remoteLabel.setForeground(Color.BLACK);
    }//GEN-LAST:event_remoteLocKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButt;
    private javax.swing.JComboBox comboFTP;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JLabel hostLabel;
    private javax.swing.JTextField hostName;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel localLabel;
    private javax.swing.JButton localLoc;
    private javax.swing.JButton okButt;
    private javax.swing.JPasswordField passwordFTP;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portNumber;
    private javax.swing.JLabel remoteLabel;
    private javax.swing.JTextField remoteLoc;
    private javax.swing.JTextField usernameFTP;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameVersi;
    private javax.swing.JLabel versiLabel;
    // End of variables declaration//GEN-END:variables
}